﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Diagnostics;

namespace EkspSys.Map
{
    class ColorPool
    {
        private Queue<Color> colors = new Queue<Color>();
        private Dictionary<Process, Color> processColorMap = new Dictionary<Process, Color>(new EkspSys.MainWindow.ProcessComparer());

        private static ColorPool instance;

        private ColorPool()
        {
            InitializeColorQueue();
        }

        private void InitializeColorQueue()
        {
            // TODO: Add more colors and place in a nice order

            colors.Enqueue(Color.Red);
            colors.Enqueue(Color.Green);
            colors.Enqueue(Color.Blue);
            colors.Enqueue(Color.Yellow);
            colors.Enqueue(Color.Purple);
            colors.Enqueue(Color.Orange);

            colors.Enqueue(Color.DarkRed);
            colors.Enqueue(Color.DarkGreen);
            colors.Enqueue(Color.DarkBlue);
            colors.Enqueue(Color.Brown);
            colors.Enqueue(Color.Indigo);
            colors.Enqueue(Color.DarkOrange);

            colors.Enqueue(Color.Pink);
            colors.Enqueue(Color.LightGreen);
            colors.Enqueue(Color.LightBlue);
            colors.Enqueue(Color.LightYellow);
            colors.Enqueue(Color.Violet);
            colors.Enqueue(Color.Moccasin);
        }

        public static ColorPool Get()
        {
            if (instance == null)
                instance = new ColorPool();
            return instance;
        }

        public Color GetColor(Process process)
        {
            if (process == null)
                return Color.White;

            if (processColorMap.ContainsKey(process))
            {
                return processColorMap[process];
            }
            else
            {
                Color c;
                if (colors.Count != 0)
                {
                    c = colors.Dequeue();
                }
                else
                {
                    c = Color.Black;
                }
                processColorMap[process] = c;
                return c;
            }
        }
    }
}
